import json
import random
from pathlib import Path
from dataclasses import dataclass
from typing import NamedTuple,Dict,Literal,Tuple,cast

class PunchProfile(NamedTuple)：
    choices: int
    threshold: int
    hit_damage: int
    block_damage: int

    pre_msg: str
    hit_msg: str
    blocked_msg: str

    knockout_possible: bool = False

    def is_hit(self):
        return random.randint(1,self.choices) <=self.threshold

@dataclass
class Player:
    name: str
    best: int
    weakness: int

    is_computer:bool

    punch_profiles: Dict[Literal[1,2,3,4],PunchProfile]
    damage: int = 0
    score: int = 0

    def get_punch_choice(self) -> Literal[1,2,3,4]:

        if self.is_computer:
            return random.randint(1,4)
        punch = -1

        while punch not in [1,2,3,4]:
            print(f"{self.name}的招式是？")

            punch = int(input())
        return punch

#击晕阈值
KNOCKOUT_THRESHOLD = 35
#击晕信息
KNOCKED_COLD = "{loser} 被击晕了无法继续进行比赛，胜利者属于{winner}"

#获取选手的弱点信息
def get_vulnerability():
    print("他的弱点是？")
    #对于数字的判断
    return  int(input())
#获取电脑的弱点信息
def get_opponent_stats() -> Tuple[int,int]:
    opponet_best = 0
    opponet_weakness = 0
    while opponet_best==opponet_weakness:
        opponet_best = random.randint(1,4)
        opponet_weakness = random.randint(1,4)

    return opponet_best,opponet_weakness

#获取招式的基本信息,这段内容不要改动
def read_punch_profiles(filepath:Path):
    with open(filepath,encoding='utf-8') as f:
        punch_profiles_dict = json.load(f)
    return {
        cast(Literal[1,2,3,4],int(key)):PunchProfile(**value)
        for key,value in punch_profiles_dict.items()
    }

def main() ->None:
    print("拳击游戏")
    print("design by xxx")
    print("由xxx设计创造")
    print("学号")

    print("你的对手名字是？")
    opponent_name = input()

    print("你的名字是？")
    player_name = input()

    print("不同的出拳策略 1摆拳 2平勾拳 3上勾拳 4刺拳")
    print("你擅长的招式是？")
    player_best = int(input())
    player_weakness = get_vulnerability()
    player=Player(
        name = player_name,
        best = player_best,
        weakness=player_weakness,
        is_computer = False,
        punch_profiles=read_punch_profiles(
            Path(__file__).parent / "player-profile.json"
        ),
    )

    opponent_best,opponent_weakness = get_opponent_stats()
    opponent = Player(
        name=player_name,
        best=player_best,
        weakness=player_weakness,
        is_computer=Ture,
        punch_profiles=read_punch_profiles(
            Path(__file__).parent / "player-profile.json"
        ),
    )
    #作一次判断，看看能不能读取对应文件的数据
    print(player)
    print(opponent)

#生成一个1-10的随机数，如果数字大于5zes对面回合
def is_opponents_turn() ->bool:
    return random.randint(1,10)>5

def play_round(round_number: int,player:Player,opponent:Player) ->None:
    print(f"第{round_number}轮次。\n开始....\n")

    if opponent.score>=2 or player.score>=2:
        return

    for _action in range(7):
        if is_opponents_turn():
            punch = opponent.get_punch_choice()
            active = opponent
            passive = player
            opponents_turn = True
        else:
            punch = player.get_punch_choice()
            active = player
            passive = opponent
            opponents_turn = False

#不用再区分是玩家攻击还是电脑攻击，只作谁主动谁被动的判断
    punch_profiles = active.punch_profiles[punch]

#如果选中的招式是擅长的招式则攻击+2
    if punch == active.best:
        active.damage = active.damage+2

        print(punch_profiles.pre_msg.format(opponent=opponent,player=player))

        #如果对手不擅长这个招式，那么他将总是被命中
        if passive.weakness == punch or punch_profiles.is_hit():
            print(punch_profiles.hit_msg.format(opponent=opponent,player=player))
            #判断是否存在击倒的可能性
            if punch_profiles.knockout_possible and active.damage>KNOCKOUT_THRESHOLD:
                if opponents_turn == False and opponent.damage>KNOCKOUT_THRESHOLD:
                    player.knockout = True
                    break
                elif opponents_turn and player.damage>KNOCKOUT_THRESHOLD:
                    opponent.knockout = True
                    break
                #如果不存在击倒可能性则记录伤害
        if opponents_turn == True:
             opponent.damage = opponent.damage+punch_profiles.hit_damage
        else:
                player.damage += punch_profiles.hit_damage

    else:
        print(punch_profiles.blocked_msg.format(opponent=opponent,player=player))
        #如果对面闪开了你的攻击，可能会因为你的攻击后摇太大对你造成反击
        if opponents_turn:
            player.damage +=punch_profiles.block_damage
        else:
            opponent.damage +=punch_profiles.block_damage
    #如果有人被击晕，直接结束比赛
    if player.knockout or opponent.knockout:
        break

    if player.damage > opponent.damage:
        print(f"{player.name}获得了第{round_number}场胜利")
    else:
        print(f"{opponent.name}获得了第{round_number}场胜利")

